//
//  UIViewController+Extension.swift
//  ImGuider X
//
//  Created by llt on 2018/9/12.
//  Copyright © 2018年 imguider. All rights reserved.
//

import UIKit
import Toast_Swift


/// 设置导航条
///
/// - black: 背景黑色 字体和按钮都是白色
/// - white: 背景白色 字体和按钮都是黑色
/// - transparency: 背景透明 字体和按钮都是白色
/// - translucence: 背景半透明(黑色) 字体和按钮都是白色 （播放页面）
/// - blackAlpha: black 加透明度
/// - whiteAlpha: white 加透明度
enum CustomBarStyle {
    case black
    case white
    case transparency
    case translucence
    case blackAlpha(CGFloat)
    case whiteAlpha(CGFloat)
}


// MARK: - 配置导航条
extension UIViewController {
    
    private struct VCKeys {
        
        static var hiddenBackItem = "hiddenBackItem"
    }
    
    var isHiddenBackItem:Bool {
        
        set {
            objc_setAssociatedObject(self, &VCKeys.hiddenBackItem, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            
            return objc_getAssociatedObject(self, &VCKeys.hiddenBackItem) as? Bool ?? false
        }
    }
    
    func setupBackItem(_ sel:Selector? = nil)  {
        
        if isHiddenBackItem {
            return
        }
        
        guard let nav = self.navigationController, nav.viewControllers.count > 1 else { return }
        
        let backItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: sel ?? #selector(backAction))
        
        self.navigationItem.leftBarButtonItem = backItem
        
        self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
    }
    @objc func setupOCVC() {
    
        setupBackItem()
    }
    
    @objc private func backAction() {
        
        self.navigationController?.popViewController(animated: true)
    }
    
    
    func setNavBarStyle(_ style:CustomBarStyle) {
        
        switch style {
            
        case .black:
            
            setBlackNav()
            
        case .white:
            
            setWhiteNav()
        case .transparency:
            
            setTransparency()
            
        case .translucence:
            
            setTranslucence()
            
        case .blackAlpha(let alpha):
            
            setAlphaNav(alpha, isWhite: false)
        case .whiteAlpha(let alpha):
            
            setAlphaNav(alpha, isWhite: true)
        }
        
    }
    private func setBlackNav() {
        
        navBarBarTintColor = kTitleColor
        navBarTintColor = UIColor.white
        navBarTitleColor = UIColor.white
        statusBarStyle = .lightContent
    }
    
    private func setWhiteNav() {
        
        navBarBarTintColor = UIColor.white
        navBarTintColor = UIColor.black
        navBarTitleColor = UIColor.black
        statusBarStyle = .default
        
    }
    
    private func setTransparency() {
        
        navBarTintColor = UIColor.white
        navBarTitleColor = UIColor.white
        statusBarStyle = .lightContent
        navBarBackgroundAlpha = 0
        navBarShadowImageHidden = true
    }
    
    private func setTranslucence() {
        
        navBarBarTintColor = kTitleColor
        navBarTintColor = UIColor.white
        navBarTitleColor = UIColor.white
        statusBarStyle = .lightContent
        navBarBackgroundAlpha = 0.34
        navBarShadowImageHidden = true
    }
    
    private func setAlphaNav(_ alpha:CGFloat, isWhite:Bool) {
        
        if self.navBarBackgroundAlpha == alpha {
            
            return
        }
        
        if isWhite {
            
            if alpha > 0.8 {
                
                setWhiteNav()
            } else {
                
                setBlackNav()
            }
            
            navBarBarTintColor = UIColor.white
            
        } else {
            
            setBlackNav()
        }
        
        UIView.animate(withDuration: 0.25) {
            
            [weak self] in
            guard let `self` = self else { return }
            
            if alpha > 0.8 {

                self.navBarShadowImageHidden = false
                self.navBarBackgroundAlpha = 1
            } else {

                self.navBarShadowImageHidden = true
                self.navBarBackgroundAlpha = 0

            }
        }
    }
    
    func closeAutomaticallyInsets(scrollview:UIScrollView) {
        
        if #available(iOS 11.0, *) {
            scrollview.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
    }
    
    @objc func makeToast(text:String?) {
        
        self.view.makeToast(text)
    }
    
    @objc func showActivity() {
        
        self.view.makeToastActivity(.center)
    }
    @objc func hiddenActivity() {
        
        self.view.hideToastActivity()
    }
}


import SKPhotoBrowser

// MARK: - 配置点击浏览大图
extension UIViewController : SKPhotoBrowserDelegate {
    
    
    private struct BrowserStoreKey {
        
        static var browserViewKey = "browserViewKey"
    }
    
    private var browserView:((_ index:Int) -> UIView?)? {
        
        set {
            
            objc_setAssociatedObject(self, &BrowserStoreKey.browserViewKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            
            return objc_getAssociatedObject(self, &BrowserStoreKey.browserViewKey) as? ((Int) -> UIView?)
        }
    }
    
    func setupBrowser(images:[Any]?,index:Int,browserView:((_ index:Int) -> UIView?)? = nil) {
        
        guard let images = images else { return }
        
        DispatchQueue.once(token: "skconfig") {
            
            SKPhotoBrowserOptions.displayAction = false
            SKPhotoBrowserOptions.displayStatusbar = true
            SKPhotoBrowserOptions.displayCounterLabel = true
            SKPhotoBrowserOptions.displayBackAndForwardButton = true
            SKPhotoBrowserOptions.enableSingleTapDismiss = true
            SKPhotoBrowserOptions.displayStatusbar = true
        }
        
        self.browserView = browserView
        
        let photos = images.compactMap { image -> SKPhoto? in
            
            if let image = image as? String  {
                
                return SKPhoto.photoWithImageURL(image, holder: nil)
            }
            
            if let image = image as? UIImage {
                
                return SKPhoto.photoWithImage(image)
            }
            
            return nil
        }
        
        let browser = SKPhotoBrowser(photos: photos, initialPageIndex: index)
        browser.cancelTitle = "取消"
        browser.delegate = self
        browser.setNavBarStyle(.transparency)
        
        self.present(browser, animated: true, completion: nil)
    }
    
    public func viewForPhoto(_ browser: SKPhotoBrowser, index: Int) -> UIView? {
        
        let view = self.browserView?(index)
        
        return view
    }
}

